package org.lease.web.service.impl;

import cn.hutool.core.util.RandomUtil;
import org.lease.common.constant.CodeConstant;
import org.lease.common.constant.RedisConstant;
import org.lease.common.exception.BizException;
import org.lease.common.holder.LoginUser;
import org.lease.common.holder.LoginUserHolder;
import org.lease.common.utils.JWTUtil;
import org.lease.model.entity.UserInfo;
import org.lease.model.enums.BaseStatus;
import org.lease.model.req.AppUserLoginRequest;
import org.lease.service.UserInfoService;
import org.lease.web.service.AliyunSMSService;
import org.lease.web.service.AppLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Service
public class AppLoginServiceImpl implements AppLoginService {
    @Autowired
    private AliyunSMSService aliyunSMSService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private UserInfoService userInfoService;

    // 发送手机验证码
    @Override
    public void sendSMSCode(String phone) {
        // 防止频繁发送验证码
        String key = RedisConstant.APP_LOGIN_CODE_PREFIX + phone;
        Boolean hasKey = stringRedisTemplate.hasKey(key);
        if (Boolean.TRUE.equals(hasKey)) {
            // key 没过期就禁止获取验证码
            throw new BizException("获取验证码过于频繁");
        }

        // 生成验证码
        String code = "9527";
        // RandomUtil.randomNumbers(CodeConstant.SMS_CODE_LENGTH);

        // 发送验证码
        // aliyunSMSService.sendSms(phone, code);

        // 保存到 Redis
        stringRedisTemplate.opsForValue()
                .set(key, code,
                        RedisConstant.APP_LOGIN_CODE_TTL_SEC,
                        TimeUnit.DAYS);
    }

    // 手机号登录
    @Override
    public String login(AppUserLoginRequest request) {
        String phone = request.getPhone();
        String code = request.getCode();
        // 校验验证码
        String key = RedisConstant.APP_LOGIN_CODE_PREFIX + phone;
        String captcha = stringRedisTemplate.opsForValue().get(key);
        if (!Objects.equals(code, captcha)) {
            throw new BizException("验证码错误");
        }
        // 查询用户
        UserInfo user = userInfoService.getByPhone(phone);
        if (user == null) {
            // 注册用户
            user = new UserInfo();
            user.setPhone(phone);
            user.setNickname(phone);
            user.setStatus(BaseStatus.ENABLE);
            userInfoService.save(user);
        } else {
            if (user.getStatus() == BaseStatus.DISABLE) {
                throw new BizException("帐号已禁用");
            }
        }
        // 生成token
        return JWTUtil.createToken(user.getId(), user.getPhone());
    }

    // 获取当前登录用户信息
    @Override
    public UserInfo getUserInfo() {
        LoginUser loginUser = LoginUserHolder.getLoginUser();
        Long userId = loginUser.getUserId();
        return userInfoService.getById(userId);
    }
}
